//
//  CXHGroMoreNativeAdapter.m
//  CXHAdSDK-GroMore
//
//  Created by 陈坤 on 2024/10/15.
//

#import "CXHGroMoreNativeAdapter.h"
#import "CXHGroMoreNativeHelper.h"
#import "CXHGroMoreNativeAd.h"
#import "CXHGroMoreNativeExpressAd.h"
#import <CXHAdSDK-Adapter/CXHAdapterNativeExpressCallBack.h>
#import <CXHAdSDK-Adapter/CXHAdapterNativeAdViewDelegate.h>

@interface CXHGroMoreNativeAdapter ()<CXHGroMoreNativeAdDelegate, CXHGroMoreNativeExpressAdDelegate>

@property (nonatomic, strong) CXHGroMoreNativeAd *nativeAd;

@property (nonatomic, strong) CXHGroMoreNativeExpressAd *nativeExpressAd;

@property (nonatomic, strong) UIView<CXHAdapterNativeExpressAdViewDelegate> *expressNativeAdView;

@end

@implementation CXHGroMoreNativeAdapter

/// 当前加载的广告的状态，native模板广告
- (BUMMediatedAdStatus)mediatedAdStatusWithExpressView:(UIView *)view {
    return BUMMediatedAdStatusUnknown;
}

/// 当前加载的广告的状态，native非模板广告
- (BUMMediatedAdStatus)mediatedAdStatusWithMediatedNativeAd:(BUMMediatedNativeAd *)ad {
    return BUMMediatedAdStatusUnknown;
}

- (void)loadNativeAdWithSlotID:(nonnull NSString *)slotID andSize:(CGSize)size imageSize:(CGSize)imageSize parameter:(nonnull NSDictionary *)parameter {
    // 获取广告加载数量
    NSLog(@"parameter %@", parameter);
    // 获取是否需要加载模板广告，非必要，视network支持而定
    NSInteger renderType = [parameter[BUMAdLoadingParamExpressAdType] integerValue];
    if (renderType == 1) { // 此处仅说明渲染类型可下发，开发者需根据实际定义情况编写
        if (self.nativeExpressAd) {
            self.nativeExpressAd.delegate = nil;
            self.nativeExpressAd = nil;
        }

        self.nativeExpressAd = [[CXHGroMoreNativeExpressAd alloc] init];
        self.nativeExpressAd.delegate = self;
        [self.nativeExpressAd loadNativeExpressAd:slotID size:size controller:self.bridge.viewControllerForPresentingModalView count:1];
        
    } else {
        
        if (self.nativeAd) {
            self.nativeAd.delegate = nil;
            self.nativeAd = nil;
        }
        
        self.nativeAd = [[CXHGroMoreNativeAd alloc] init];
        self.nativeAd.delegate = self;
        
        [self.nativeAd loadNativeAd:slotID controller:self.bridge.viewControllerForPresentingModalView count:1];
    }
}

- (void)registerContainerView:(nonnull __kindof UIView *)containerView andClickableViews:(nonnull NSArray<__kindof UIView *> *)views forNativeAd:(nonnull id)nativeAd {
    UIView<CXHAdapterNativeAdViewDelegate> *nativeAdView = (UIView<CXHAdapterNativeAdViewDelegate> *)nativeAd;
    [containerView insertSubview:nativeAdView atIndex:0];
    nativeAdView.frame = containerView.bounds;
    [nativeAdView cxh_registViews:views];
}

- (void)renderForExpressAdView:(nonnull UIView *)expressAdView {
    UIView<CXHAdapterNativeExpressAdViewDelegate> *adView = (UIView<CXHAdapterNativeExpressAdViewDelegate> *)expressAdView;
    [adView render];
    _expressNativeAdView = adView;
}

- (void)setRootViewController:(nonnull UIViewController *)viewController forExpressAdView:(nonnull UIView *)expressAdView {
//    if ([expressAdView isKindOfClass:[BUMDCustomExpressNativeView class]]) {
//        BUMDCustomExpressNativeView *view = (BUMDCustomExpressNativeView *)expressAdView;
//        view.viewController = viewController;
//    }
}

- (void)setRootViewController:(nonnull UIViewController *)viewController forNativeAd:(nonnull id)nativeAd {
    UIView<CXHAdapterNativeAdViewDelegate> *nativeAdView = (UIView<CXHAdapterNativeAdViewDelegate> *)nativeAd;
    [nativeAdView cxh_setRootViewController:viewController];
}

- (void)didReceiveBidResult:(BUMMediaBidResult *)result {
    // 在此处理Client Bidding的结果回调
    if (result.win) {
        if (self.nativeAd) {
            NSInteger winPrice = self.nativeAd.getBidPrice;
            if (result.winnerPrice) {
                winPrice = result.winnerPrice;
            }
            [self.nativeAd sendWinNotice:winPrice];
        }
        if (self.nativeExpressAd) {
            NSInteger winPrice = self.nativeExpressAd.getBidPrice;
            if (result.winnerPrice) {
                winPrice = result.winnerPrice;
            }
            [self.nativeExpressAd sendWinNotice:winPrice];
        }
    } else {
        if (result.winnerPrice) {
            if (self.nativeAd) {
                [self.nativeAd sendLossNotice:result.winnerPrice];
            }
            if (self.nativeExpressAd) {
                [self.nativeExpressAd sendLossNotice:result.winnerPrice];
            }
        }
    }
}

#pragma mark - CXHGroMoreNativeExpressAdDelegate
/**
 信息流广告请求成功

 @param nativeExpressAd 广告实例
 @param adViewArray 广告模板集合
 */
- (void)CXHGroMore_nativeExpressAdSucessToLoad:(CXHAdSDKNativeExpressAd *)nativeExpressAd
                                   adViewArray:(NSArray<__kindof UIView<CXHAdapterNativeExpressAdViewDelegate> *> *)adViewArray{
    NSMutableArray *exts = [NSMutableArray arrayWithCapacity:1];
    NSString *ecpm = [NSString stringWithFormat:@"%ld", nativeExpressAd.getBidPrice];
    [exts addObject:@{BUMMediaAdLoadingExtECPM : ecpm,}];
    [self.bridge nativeAd:self didLoadWithExpressViews:[adViewArray copy] exts:exts.copy];
}

/**
 信息流广告请求失败

 @param nativeExpressAd 广告实例
 @param error 请求错误描述
 */
- (void)CXHGroMore_nativeExpressAdFailToLoad:(CXHAdSDKNativeExpressAd *)nativeExpressAd
                                       error:(NSError *)error{
    [self.bridge nativeAd:self didLoadFailWithError:error];
}

/**
 信息流广告渲染成功

 @param adView 广告视图
 */
- (void)CXHGroMore_nativeExpressAdViewRenderOrRegistSuccess:(CXHAdSDKNativeExpressAd *)nativeExpressAd adView:(UIView<CXHAdapterNativeExpressAdViewDelegate> *)adView{
    [self.bridge nativeAd:self renderSuccessWithExpressView:adView];
}

/**
 信息流广告渲染失败

 @param adView 广告视图
 */
- (void)CXHGroMore_nativeExpressAdViewRenderOrRegistFail:(CXHAdSDKNativeExpressAd *)nativeExpressAd adView:(UIView<CXHAdapterNativeExpressAdViewDelegate> *)adView{
    
}

/**
 信息流广告被点击

 @param nativeExpressAd 广告模板
 */
- (void)CXHGroMore_nativeExpressAdClicked:(CXHAdSDKNativeExpressAd *)nativeExpressAd
                                   adView:(__kindof UIView<CXHAdapterNativeExpressAdViewDelegate> *)adView loadingPageURL:(NSString *)loadingPageURL{
    
    [self.bridge nativeAd:self didClickWithMediatedNativeAd:adView];
    [self.bridge nativeAd:self willPresentFullScreenModalWithMediatedNativeAd:adView];
    
}

/**
 信息流广告被关闭
 
 @param nativeExpressAd 广告模板
 */
- (void)CXHGroMore_nativeExpressAdClose:(CXHAdSDKNativeExpressAd *)nativeExpressAd
                                 adView:(__kindof UIView<CXHAdapterNativeExpressAdViewDelegate> *)adView{
    
    [self.bridge nativeAd:self didCloseWithExpressView:adView closeReasons:@[]];
}

/**
 信息流广告被展示
 
 @param nativeExpressAd 广告实例
 */
- (void)CXHGroMore_nativeExpressAdExposure:(CXHAdSDKNativeExpressAd *)nativeExpressAd
                                    adView:(__kindof UIView<CXHAdapterNativeExpressAdViewDelegate> *)adView{
    [self.bridge nativeAd:self didVisibleWithMediatedNativeAd:adView];
}

/**
 信息流广告展示上报
 
 @param nativeExpressAd 广告实例
 */
- (void)CXHGroMore_nativeExpressAdExposureReport:(CXHAdSDKNativeExpressAd *)nativeExpressAd
                                          adView:(__kindof UIView<CXHAdapterNativeExpressAdViewDelegate> *)adView{
    
}

/**
 信息流广告点击上报

 @param nativeExpressAd 广告模板
 */
- (void)CXHGroMore_nativeExpressAdClickedReport:(CXHAdSDKNativeExpressAd *)nativeExpressAd
                                         adView:(__kindof UIView<CXHAdapterNativeExpressAdViewDelegate> *)adView{
}

/**
 信息流广告落地页被关闭
 
 @param nativeExpressAd 广告实例
 */
- (void)CXHGroMore_nativeExpressAdCloseLandingPage:(CXHAdSDKNativeExpressAd *)nativeExpressAd
                                            adView:(__kindof UIView<CXHAdapterNativeExpressAdViewDelegate> *)adView{
    [self.bridge nativeAd:self willPresentFullScreenModalWithMediatedNativeAd:adView];
}

#pragma mark - CXHGroMoreNativeAdDelegate
/**
 信息流原生广告请求成功

 @param nativeAd 广告实例
 @param adViewArray 广告模板集合
 */
- (void)CXHGroMore_nativeAdSucessToLoad:(CXHAdSDKNativeAd *)nativeAd
                            adViewArray:(NSArray<__kindof UIView<CXHAdapterNativeAdViewDelegate> *> *)adViewArray{
    NSMutableArray<BUMMediatedNativeAd *> *mediatedNativeAds = [NSMutableArray array];
    for (UIView<CXHAdapterNativeAdViewDelegate> *nativeAdView in adViewArray) {
        
        id<BUMMediatedNativeAdData, BUMMediatedNativeAdViewCreator> helper = [[CXHGroMoreNativeHelper alloc] initWithAdData:nativeAdView];
        BUMMediatedNativeAd *ad = [[BUMMediatedNativeAd alloc] init];
        ad.originMediatedNativeAd = nativeAdView;
        ad.view = nativeAdView;
        ad.viewCreator = helper;
        ad.data = helper;
        [mediatedNativeAds addObject:ad];
    }
    NSMutableArray *exts = [NSMutableArray arrayWithCapacity:1];
    NSString *ecpm = [NSString stringWithFormat:@"%ld", nativeAd.getBidPrice];
    [exts addObject:@{BUMMediaAdLoadingExtECPM : ecpm,}];
    [self.bridge nativeAd:self didLoadWithNativeAds:mediatedNativeAds exts:exts.copy];
}

/**
 信息流广告请求失败

 @param nativeAd 广告实例
 @param error 请求错误描述
 */
- (void)CXHGroMore_nativeAdFailToLoad:(CXHAdSDKNativeAd *)nativeAd
                                error:(NSError *)error{
    [self.bridge nativeAd:self didLoadFailWithError:error];
}

/**
 信息流广告被点击

 @param nativeAd 广告模板
 */
- (void)CXHGroMore_nativeAdClicked:(CXHAdSDKNativeAd *)nativeAd
                            adView:(__kindof UIView<CXHAdapterNativeAdViewDelegate> *)adView loadingPageURL:(NSString *)loadingPageURL{
    
    [self.bridge nativeAd:self didClickWithMediatedNativeAd:adView];
    [self.bridge nativeAd:self willPresentFullScreenModalWithMediatedNativeAd:adView];
}

/**
 信息流广告被关闭
 
 @param nativeAd 广告模板
 */
- (void)CXHGroMore_nativeAdClose:(CXHAdSDKNativeAd *)nativeAd
                    adView:(__kindof UIView<CXHAdapterNativeAdViewDelegate> *)adView{
    [self.bridge nativeAd:self adContainerViewDidRemovedWithNativeAd:adView];
}

/**
 信息流广告被展示
 
 @param nativeAd 广告实例
 */
- (void)CXHGroMore_nativeAdExposure:(CXHAdSDKNativeAd *)nativeAd
                             adView:(__kindof UIView<CXHAdapterNativeAdViewDelegate> *)adView{
    
    [self.bridge nativeAd:self didVisibleWithMediatedNativeAd:adView];
}

/**
 信息流广告展示上报
 
 @param nativeAd 广告实例
 */
- (void)CXHGroMore_nativeAdExposureReport:(CXHAdSDKNativeAd *)nativeAd
                                   adView:(__kindof UIView<CXHAdapterNativeAdViewDelegate> *)adView{
    
}

/**
 信息流广告点击上报

 @param nativeAd 广告模板
 */
- (void)CXHGroMore_nativeAdClickedReport:(CXHAdSDKNativeAd *)nativeAd
                                  adView:(__kindof UIView<CXHAdapterNativeAdViewDelegate> *)adView{
    
}

/**
 信息流广告落地页被关闭
 
 @param nativeAd 广告实例
 */
- (void)CXHGroMore_nativeAdCloseLandingPage:(CXHAdSDKNativeAd *)nativeAd
                                     adView:(__kindof UIView<CXHAdapterNativeAdViewDelegate> *)adView{
    [self.bridge nativeAd:self didDismissFullScreenModalWithMediatedNativeAd:adView];
}

@end
